package com.masterclient.app.businessmodule.settingmodule;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eoner.common.config.Config;
import com.eoner.managerlibrary.system.SystemConfigStorage;
import com.eoner.managerlibrary.user.UserDataManager;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.minemodule.cash.BinderMobileActivity;
import com.masterclient.app.businessmodule.usermodule.login.beans.LoginBean;
import com.masterclient.app.businessmodule.usermodule.login.contract.BringPwdBackContract;
import com.masterclient.app.businessmodule.usermodule.login.contract.BringPwdBackPresenter;
import com.masterclient.app.utils.CaptchaUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：sean
 * 创建时间：2017/8/26 15:35
 * 功能说明：
 */

public class ModifyPwdActivity extends BaseActivity<BringPwdBackPresenter> implements BringPwdBackContract.View {

    @BindView(R.id.rl_back)
    RelativeLayout rlBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_head)
    RelativeLayout rlHead;
    @BindView(R.id.tv_tag_area)
    TextView tvTagArea;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_verify)
    TextView tvVerify;
    @BindView(R.id.tv_tag_verify)
    TextView tvTagVerify;
    @BindView(R.id.et_verify)
    EditText etVerify;
    @BindView(R.id.tv_tag_pwd)
    TextView tvTagPwd;
    @BindView(R.id.et_pwd)
    EditText etPwd;
//    @BindView(R.id.checkBox)
//    CheckBox checkBox;
//    @BindView(R.id.rl_show_pwd)
//    RelativeLayout rlShowPwd;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    private String verifyCode;
    private String pwd, phoneNum;

    CaptchaUtil captchaUtil;

    String captcha_type = "reset_password";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_password);
        ButterKnife.bind(this);
        phoneNum = UserDataManager.getInstance().getMobile();
        if (TextUtils.isEmpty(phoneNum)) {
            startActivitry(BinderMobileActivity.class, null);
            finish();
        }

        if (!TextUtils.isEmpty(UserDataManager.getInstance().getMobile())) {
            tvPhone.setText(UserDataManager.getInstance().getMobile().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
        }

        etVerify.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() >= 4 && etPwd.getText().toString().length() >= 6) {
                    tvConfirm.setClickable(true);
                    tvConfirm.setBackgroundResource(R.drawable.bg_rect_buy_black);
                    tvConfirm.setTextColor(0xffffffff);
                } else {
                    tvConfirm.setClickable(false);
                    tvConfirm.setBackgroundResource(R.drawable.all_round_grey);
                    tvConfirm.setTextColor(0xffc1c1c1);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        etPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (etVerify.getText().toString().trim().length() >= 4 && s.length() >= 6) {
                    tvConfirm.setClickable(true);
                    tvConfirm.setBackgroundResource(R.drawable.bg_rect_buy_black);
                    tvConfirm.setTextColor(0xffffffff);
                } else {
                    tvConfirm.setClickable(false);
                    tvConfirm.setBackgroundResource(R.drawable.all_round_grey);
                    tvConfirm.setTextColor(0xffc1c1c1);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        captchaUtil = new CaptchaUtil(mContext, "修改密码");

        captchaUtil.setCaptchaListerner(new CaptchaUtil.CaptchaListerner() {
            @Override
            public void onSuccess(String ne) {
                tvVerify.setClickable(false);
                mPresenter.getCaptcha(UserDataManager.getInstance().getMobile(), captcha_type, ne);
            }

            @Override
            public void onFailed() {
                tvVerify.setClickable(true);
            }
        });
    }

    @Override
    protected void onPause() {
        hideSoftInput(etPwd);
        super.onPause();
    }

    @OnClick({R.id.rl_back, R.id.tv_verify, R.id.tv_confirm})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.tv_verify:
                if (SystemConfigStorage.getInstance().isOpenSlipPageCode()) {
                    if (CaptchaUtil.CAPTCHA_NUMBER_TIMES >= Config.CAPTCHA_MAX_TIMES) {
                        mPresenter.getCaptcha(phoneNum, captcha_type, "");
                    } else {
                        captchaUtil.start();
                    }
                } else {
                    mPresenter.getCaptcha(phoneNum, captcha_type, "");
                    tvVerify.setClickable(false);
                }
                break;
//            case R.id.rl_show_pwd:
//                if (checkBox.isChecked()) {
//                    checkBox.setChecked(false);
//                    etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
//                } else {
//                    checkBox.setChecked(true);
//                    etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
//                }
//                break;
            case R.id.tv_confirm:
                verifyCode = etVerify.getText().toString().trim();
                pwd = etPwd.getText().toString().trim();
                if (!TextUtils.isEmpty(pwd) && pwd.length() < 6) {
                    showToast("密码长度不能小于6位");
                } else {
                    mPresenter.forgotSetPwd(verifyCode, phoneNum, pwd, captcha_type);
                }
                break;
        }
    }

    @Override
    public void setPwdSuccess() {
        showToast("重置密码成功");
        finish();
    }

    @Override
    public void setPwdFailed(String msg) {
        showToast(msg);
    }

    @Override
    public void getCaptchaSuccess() {
        tvVerify.setBackgroundResource(R.drawable.order_btn_bg_gray);
        tvVerify.setTextColor(0xff999999);
        CountDownTimer timer = new CountDownTimer(60 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvVerify.setText(millisUntilFinished / 1000 + "s");
            }

            @Override
            public void onFinish() {
                tvVerify.setClickable(true);
                tvVerify.setBackgroundResource(R.drawable.order_btn_red);
                tvVerify.setTextColor(0xffcc363c);
                tvVerify.setText("获取验证码");
            }
        };
        timer.start();
    }

    @Override
    public void getCaptchaFailed(String msg) {
        showToast(msg);
        tvVerify.setClickable(true);
        tvVerify.setBackgroundResource(R.drawable.order_btn_red);
        tvVerify.setTextColor(0xffcc363c);
        tvVerify.setText("获取验证码");
    }

    @Override
    public void loginSuccess(LoginBean.LoginData data) {

    }

    @Override
    public void loginFailed() {

    }

}
